<?php

namespace Install\Controller;

use Think\Controller;
use Think\Storage;

/**
 * @author ZXZ <zxz054321@163.com>
 */
class EmptyController extends Controller {

    public function index() {
        if (!defined('BIND_MODULE') || BIND_MODULE != 'Install') {
            $this->error('安装程序打开方式不对！');
            exit;
        }

        $lockFile = './install.lock';
        if (!APP_DEBUG)
            if (Storage::has($lockFile)) {
                $this->error('不可重复安装！');
            }

        $this->installModules();
        $this->installAddons();

        // 写入安装锁定文件
        Storage::put($lockFile, 'lock');
    }

    private function installModules() {
        $class = get_module_class('Install');
        $module = new $class;
        $module->install();

        $moduleList = array(
//            'ModuleName',
        );
        $db = M('Modules');
        foreach ($moduleList as $module) {
            $class = get_module_class($module);
            $module = new $class;

            $module->install();

            if ($module->checkInfo()) {
                $db->create($module->info);
                $db->add();
            }
        }
    }

    private function installAddons() {
        $list = array(
//            'AddonName',
        );
        $db = M('Addons');
        foreach ($list as $addon) {
            $class = get_addon_class($addon);
            $addon = new $class;

            $addon->install();

            if ($addon->checkInfo()) {
                $db->create($addon->info);
                if ($db->add()) {
                    D('Hooks')->updateHooks($addon->info['name']);
                }
            }
        }
    }

}
